﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.OleDb;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class panel_F5Part : UserControl
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        public panel_F5Part()
        {
            InitializeComponent();
            mySetting = new INISettings();
            myConnection = new SqlConnection(mySetting.SQLConnectionString);
            LoadData();
            this.F5Part.DoubleClick += F5Part_DoubleClick;
            this.F5Part.PopupMenuShowing += F5Part_PopupMenuShowing;
            this.f5PartContextMenu.ItemClicked += f5PartContextMenu_ItemClicked;
        }

        ////////////////////////////////
        /// Call Edit Form
        ////////////////////////////////
        private void callEditForm()
        {
            DataRow row = F5Part.GetFocusedDataRow();
            frm_Edit_F5Part editForm = new frm_Edit_F5Part();
            editForm.partno = row[0].ToString();
            if (editForm.LoadData())
            {
                editForm.ShowDialog();
                LoadData();
            }
        }

        void f5PartContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.f5PartContextMenu.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callEditForm();
                    break;
                case "addSelection":
                    frm_Add_F5Part add_Form = new frm_Add_F5Part();
                    add_Form.ShowDialog();
                    LoadData();
                    break;
                case "deleteSelection":
                    deleteRecord();
                    break;
                case "refreshSelection":
                    LoadData();
                    break;
            }
        }

        void F5Part_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.f5PartContextMenu.Show(MousePosition);
        }

        private void F5Part_DoubleClick(object sender, EventArgs e)
        {
            callEditForm(); 
        }
        
        //////////////////////////////////////////////////
        /// Load data from database to grid
        //////////////////////////////////////////////////
        private void LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySetting.SQLConnectionString;
            string sql = "SELECT PartNo, F5Product, F5Level, F5Name, AssGroup, AssRemark, AssSerial,"
                + " AssSTDTime FROM [222_Part] WHERE F5 = 1 ORDER BY PartNo";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridF5Part.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("PartNo", typeof(string));
                newTable.Columns.Add("Product", typeof(string));
                newTable.Columns.Add("Level", typeof(int));
                newTable.Columns.Add("Name", typeof(string));
                newTable.Columns.Add("Group", typeof(string));
                newTable.Columns.Add("AssRemark", typeof(string));
                newTable.Columns.Add("Serial", typeof(string));
                newTable.Columns.Add("AssSTDTime", typeof(float));                
                gridF5Part.DataSource = newTable;
            }
            F5Part.Columns[0].Caption = "Part No";
            F5Part.Columns[1].Caption = "Product";
            F5Part.Columns[2].Caption = "Level";
            F5Part.Columns[3].Caption = "Name";
            F5Part.Columns[4].Caption = "Group";
            F5Part.Columns[5].Caption = "Remark";
            F5Part.Columns[6].Caption = "Serial";
            F5Part.Columns[7].Caption = "STD Time";
            F5Part.BestFitColumns();
        }

        private void bottonAdd_Click(object sender, EventArgs e)
        {            
            frm_Add_F5Part add_Form = new frm_Add_F5Part();
            add_Form.ShowDialog();
            LoadData();
        }

        private void bottonEdit_Click(object sender, EventArgs e)
        {
            callEditForm(); 
        }

        private void deleteRecord()
        {
            int[] rows = F5Part.GetSelectedRows();
            if (rows.Length <= 0)
            {
                MessageBox.Show("There is no selected record to delete");
            }
            else
            {
                if (MessageBox.Show("Are you really to delete selected records now?", "Confirm delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    string[] id_list = new string[rows.Length];
                    for (int i = 0; i < rows.Length; i++)
                    {
                        DataRow selected_row = F5Part.GetDataRow(rows[i]);
                        id_list[i] = "'" + selected_row[0].ToString() + "'";
                    }
                    string id = string.Join(",", id_list);

                    try
                    {
                        myConnection.Open();
                        string sql = "UPDATE [222_Part] SET F5Product = NULL, F5Level = NULL, F5Name = NULL,"
                            + " AssGroup = NULL, AssRemark = NULL, AssSerial = NULL, AssSTDTime = NULL, F5 = 0"
                            + " WHERE PartNo IN (" + id + ")";
                        SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                        cmd_sql.ExecuteNonQuery();
                        cmd_sql.Dispose();
                        myConnection.Close();
                        MessageBox.Show("F5 Part List deleted success.");
                        LoadData();
                    }
                    catch //(Exception ex)
                    {
                        if (myConnection.State == ConnectionState.Open)
                        {
                            myConnection.Close();
                        }
                        //MessageBox.Show(ex.Message);
                        MessageBox.Show("Cannot delete F5 Part List. Please try again.");
                    }
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            deleteRecord();
        }
    }
}
